#!/bin/bash

# Slackware build script for guitarix
# Originally written by Michales Michaloudes (email removed)

# Modified by B. Watson <yalhcru@gmail.com>. Modified version released
# under the WTFPL, for details see http://www.wtfpl.net/txt/copying/

# Note to self: git URL for guitarix is:
# git://git.code.sf.net/p/guitarix/git/

# 20211205 bkw: BUILD=2
# - new-style icons.
# - correct install permissions in /usr/share/gx_head
# - who the heck is kb6?
# 20200408 kb6: Updated for 0.42.1. Add -fpermissive compiler flag.
# 20201105 bkw: Add no-upgrade note to README.
# 20200307 bkw: Updated for 3.39.0.
# 20191211 bkw: Updated for 0.38.1.
# 20180828 bkw: Updated for 0.37.3.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=guitarix
VERSION=${VERSION:-0.42.1}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# 20220322 bkw: 32-bit builds need -mfxsr now.
if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686 -msse -mfxsr"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686 -msse -mfxsr"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

# Tarballs are now named "guitarix2-$VERSION", but the dir inside the
# tarball is still "guitarix-$VERSION".
TARNAME=${PRGNAM}2

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$TARNAME-$VERSION.tar.xz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# 20211205 bkw: why? *WHY?!*
sed -i '/chmod/s,0o664,0o644,g' wscript

# guitarix wants to install the roboto font in the wrong place for
# slackware.
sed -i '/fonts/s,\<truetype\>,TTF,' wscript tools/undiag

# this is no longer documented in README, but still here for debugging.
if [ "${LV2:-yes}" = "no" ]; then
  LV2OPT=--no-lv2
fi

# grrr, -DNDEBUG doesn't get added to the flags automatically...
LIBDIR="/usr/lib${LIBDIRSUFFIX}"
./waf configure \
  --prefix=/usr \
  --cxxflags-release="$SLKCFLAGS -DNDEBUG -fpermissive" \
  --ladspadir="$LIBDIR/ladspa/" \
  --lv2dir="$LIBDIR/lv2/" \
  --libdir="$LIBDIR" \
  --no-avahi \
  --no-ldconfig \
  --no-desktop-update \
  --no-faust \
  --includeresampler \
  --includeconvolver \
  --ladspa \
  --new-ladspa \
  --mod-lv2 \
  $EXTRAOPT \
  $LV2OPT
./waf build # -v # verbose, shows actual commands
./waf install --destdir=$PKG

# Dunno how to make waf strip stuff...
strip $PKG/usr/bin/* $PKG/usr/lib*/{,ladspa/*,lv2/*/*}*.so \

# --install-roboto-font does the wrong thing, we'll install it manually.
mkdir -p $PKG/usr/share/fonts/TTF
install -m0644 fonts/* $PKG/usr/share/fonts/TTF

# Shared libraries are supposed to be +x.
chmod 755 $PKG/$LIBDIR/libgx*.so.?.?

# Man page written for this SlackBuild, see guitarix.rst.
mkdir -p $PKG/usr/man/man1
gzip -9c < $CWD/$PRGNAM.1 > $PKG/usr/man/man1/$PRGNAM.1.gz

# for the 128x128 icon, center upstream's 100x100 icon in a 128x128 png.
dir=$PKG/usr/share/icons/hicolor/128x128/apps
mkdir -p $dir
convert -background none -extent 128x128 -gravity center \
        pixmaps/gx_head.png \
        $dir/$PRGNAM.png

# for the rest, resize as usual.
for px in 16 32 48 64; do
  size=${px}x${px}
  dir=$PKG/usr/share/icons/hicolor/$size/apps
  mkdir -p $dir
  convert -resize $size \
          pixmaps/gx_head.png \
          $dir/$PRGNAM.png
done

ln -s ../icons/hicolor/48x48/apps/$PRGNAM.png $PKG/usr/share/pixmaps/$PRGNAM.png

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING changelog README* $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh  > $PKG/install/doinst.sh

# Only add capability stuff if not disabled:
if [ "${SETCAP:-yes}" = "yes" ]; then
  cat $CWD/setcap.sh >> $PKG/install/doinst.sh
  # Only allow execution by audio group
  chown root:audio $PKG/usr/bin/$PRGNAM
  chmod 0750 $PKG/usr/bin/$PRGNAM
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
